#!/usr/bin/env python
# -*-coding:UTF-8-*-
# Author: chenzuoqing@126.com
# Create: 2018/8/31 16:47
# File  : filter.py

"""
工单过滤类
"""

import django_filters
from django.db.models import Q
from workorder.models import WorkOrder


class WorkOrderFilter(django_filters.FilterSet):
    """
    根据前端用户自己的id，返回用户自己的数据
    """
    user = django_filters.CharFilter(method='id_filter')

    @staticmethod
    def id_filter(queryset, name, value):
        return queryset.filter(Q(applicant__id__exact=value) | Q(assign_to__id__exact=value))

    class Meta:
        model = WorkOrder
        fields = ['applicant', 'assign_to', 'id']
